package com.playcrab.adouban;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gdata.client.douban.DoubanService;
import com.google.gdata.data.douban.Attribute;
import com.google.gdata.data.douban.SubjectEntry;
import com.google.gdata.util.ServiceException;
import com.playcrab.adouban.util.DoubanAPI;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.webkit.WebView; 
import android.widget.LinearLayout;
import android.widget.TextView;

public class ViewMovie extends ViewSubject {
	 
	private static final String LOG_TAG = "ViewMovie";
 	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle bundle = this.getIntent().getExtras();		
		id = bundle.getString("id");
		type = bundle.getString("type");
		Log.v(LOG_TAG, "id:" + id );
 		//setContentView(R.layout.view_movie); 
 		this.showPending();

		new Thread() {
			public void run() { 
				try {
					ViewMovie.this.getSubject();
					handler.post(new Runnable() {
						public void run() {
							ViewMovie.this.showSubject();
							ViewMovie.this.showReview();
							ViewMovie.this.hidePending();
						}
					});
					
				} catch (IOException e) { 
					e.printStackTrace(); 
					ViewMovie.this.showInfo(getString(R.string.network_error));
				} catch (ServiceException e) {
					e.printStackTrace();
					ViewMovie.this.showInfo(getString(R.string.service_error));
				} finally{
					
				}
			}

			
		}.start();
		
	}
	
 	@Override
 	public SubjectEntry getSubject() throws IOException, ServiceException{
		DoubanService service = DoubanAPI.getInstance().getService();
		Log.v(LOG_TAG, "id:" + id );
		subject = service.getMovie(id);
		reviewFeed = service.getMovieReviews(id, 1, 5, "score");
		return subject;
 	}
	 
 	@Override
	public void showSubject(){
		if(subject == null){
			return;
		}
		
		LayoutInflater inflater = LayoutInflater.from(this);
		subjectHeader = inflater.inflate(R.layout.view_movie, null);
		
		if (subject.getTitle() != null) {
			TextView title = (TextView)  subjectHeader.findViewById(R.id.movie_title);
			title.setText(subject.getTitle().getPlainText());
		}
		
		LinearLayout movie_info = (LinearLayout) subjectHeader.findViewById(R.id.movie_info); 
		movie_info.addView(newRatingView()); 
		 
		Map<String, List<String>>  attributes = DoubanAPI.getInstance().buildAttributes(subject.getAttributes());
		
		movie_info.addView(newAttributeView(attributes,"director","导演:  ")); 
		movie_info.addView(newAttributeView(attributes,"cast","主演:  "));  
		movie_info.addView(newAttributeView(attributes,"writer","编剧:  "));  
		movie_info.addView(newAttributeView(attributes,"pubdate","上映日期:  "));  
		movie_info.addView(newAttributeView(attributes,"country","制作国家/地区:  "));  
		movie_info.addView(newAttributeView(attributes,"language","语言:  "));  
		movie_info.addView(newAttributeView(attributes,"aka","又名:  "));  
		movie_info.addView(newAttributeView(attributes,"imdb","IMDB:  "));   
		
		if (subject.getSummary() != null) {
			((TextView) subjectHeader.findViewById(R.id.movie_summary)).setText(subject
					.getSummary().getPlainText());
		}
		
		if (subject.getLink("image", null) != null) {
			WebView  image = (WebView) subjectHeader.findViewById(R.id.movie_image);
			image.loadUrl(subject.getLink("image", null).getHref());
		}
		
		
		
		
	}

}
